home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 05 - 1989 / 05.12 Dec 89 / ASM Program / Assembly Language Code next >
Encoding:
Text File  |  1988-07-01  |  5.2 KB  |  242 lines  |  [TEXT/QED1]

  1.     MC68881        ; We will be using
  2.             ; instructions for
  3.             ; the 68881 chip
  4.     MACHINE MC68020                ; optimize for the
  5.             ; 68020, if you don't
  6.             ; have one remove
  7.             ; this line
  8.     
  9. ScaleMult     FUNC     EXPORT
  10.             ; this lets the rest
  11.             ; of the world know
  12.             ; about our function
  13.  
  14. _Debugger  OPWORD $A9FF            ; this will pop us
  15.             ; into Macs bugs so
  16.             ; we can follow every
  17.             ; instruction and
  18.             ; observe the 
  19.             ; registers
  20.  
  21.  
  22. ; The following are all displacements so we
  23. ; can address our data on the stack, relative
  24. ; to the address stored in A6
  25.  
  26. result    EQU    20            ; two bytes for our
  27.             ; integer result
  28. Scalar    EQU    16            ; 4 bytes (address of
  29.             ; the scalar)
  30. inMat    EQU    12    ; 4 bytes (address of
  31.             ; the input Matrix)
  32. outMat    EQU    8
  33.             ; 4 bytes (address of
  34.             ; the output Matrix)
  35. ReturnAdd     EQU    4            ; 4 bytes
  36. superScalar EQU    -12
  37.             ; 12 bytes (hold a
  38.             ; place for our 68881
  39.             ; version of the
  40.             ; scalar)
  41. oldA2    EQU     -14                ; old the old value
  42.             ; of A2
  43. oldA3    EQU    -16    ; old the old value
  44.             ;of A3
  45.  
  46.  
  47. ; *** Add the following features (to make
  48. ; this a “real” program):
  49. ;         1) make sure the number of 
  50. ;        rows and columns in the input ;        Matrix jives with that of the ;        the output matrix.
  51. ;        2) Fix the error message to 
  52. ;        tell the Pascal Program if 
  53. ;        there was a problem, such as 
  54. ;        if: rows X columns ≠ vector 
  55. ;        size
  56.  
  57. ; ***** The first “real” line of our program ;         (it’s about time!)
  58.  
  59.     _Debugger    ; jump into Macsbugs
  60.             ; so we can follow
  61.             ; what's going on
  62.  
  63. ; move all the initial parameters off the 
  64. ; stack from the calling routine "function 
  65. ; ScaleMult (scalar:extended;var inMatrix,
  66. ; outMatrix:matrix) :error"
  67.             
  68.     link    A6,#-12    ; push A6 (the frame
  69.             ; pointer) onto the
  70.             ; stack , load SP
  71.             ; into A6, then
  72.             ; subtract off 12
  73.             ; bytes from A7 to
  74.             ; make room for our
  75.             ; variables
  76.  
  77. ; *******************************************
  78. ; after the link instruction
  79. ; the stack looks as follows:
  80. ;
  81. ; high
  82. ;        integer (2 bytes)
  83. ;        ------------    <- 20(A6)
  84. ;        scalar (4 bytes)
  85. ;        ------------    <- 16(A6)
  86. ;        inMatrix (4 bytes)
  87. ;        ------------    <- 12(A6)
  88. ;        oldMatrix (4 bytes)
  89. ;        ------------    <-  8(A6)
  90. ;        returnAddress (4 bytes)
  91. ;        ------------    <-  4(A6)
  92. ;        old A6 (4 bytes)
  93. ;        ------------    <-  (A6)
  94. ;     superscalar (12 bytes)
  95. ;        ------------    <- -12(A6) 
  96. ;                 and initial (A7)
  97. ;    low        
  98. ;
  99. ; *******************************************
  100.  
  101.  
  102.             
  103. ; now move the addresses onto the chip so we ; can work with them
  104.  
  105.     MOVE.L    A2,-(A7); push A2 onto the
  106.             ; stack to save it
  107.     MOVE.L    A3,-(A7); do the same to A3
  108.     MOVE.L    inMat(A6),A2
  109.             ; get the pointer to
  110.             ; the address of
  111.             ; inMatrix
  112.     MOVE.L    4(A2),A0            ; get the address of
  113.             ; the input matrix
  114.             ; (4 bytes from the
  115.             ; start of our matrix
  116.             ; data structure )
  117.     MOVE.L    outMat(A6),A2    
  118.     MOVE.L    4(A2),A1; get the address of
  119.             ; the output matrix
  120.     MOVE.L    inMat(A6),A2            ; get the number of
  121.             ; rows (first byte of
  122.             ; Matrix record)
  123.     MOVE.W    (A2),D0
  124.     MOVE.W    2(A2),D1; then get the number
  125.             ; of columns (3rd 
  126.             ; byte of matrix
  127.             ; record
  128.  
  129.     MOVE.L    scalar(A6),A2
  130.             ; we will now move
  131.             ; the scalar to a
  132.             ; place with a bit
  133.             ; more room
  134.     LEA    superScalar(A6),A3
  135.     CLR.W    (A3)+
  136.     MOVE.L    (A2)+,(A3)+ ; move the first 4
  137.             ; bytes
  138.  
  139.     MOVE.L    (A2)+,(A3)+; move the next 4
  140.             ; bytes
  141.  
  142.     MOVE.W    (A2),(A3); move the last byte
  143.             
  144.     LEA    superScalar(A6),A2
  145.     MOVE.L    (A2),D2    ; get the top
  146.             ; long-word of
  147.             ; the scalar, shift
  148.             ; it, then put it
  149.             ; back
  150.     LSL.L    #8,D2
  151.     LSL.L    #8,D2
  152.     MOVE.L    D2,(A2)
  153.  
  154.     FMOVE.X    (A2),FP1; get the modified
  155.             ; scalar and store it
  156.             ; on the 68881
  157.             
  158. Loop    MOVE.L    (A0),D2    ; get the current
  159.             ; high Long word of
  160.             ; the element from
  161.             ; the input matrix
  162.     LSL.L    #8,D2
  163.     LSL.L    #8,D2
  164.     MOVE.L    D2,(A0)
  165.             
  166.     FMOVE.X    (A0),FP0; get the current
  167.             ; element
  168.     FMUL.X    FP1,FP0    ; multiply by the
  169.             ; scalar
  170.     FMOVE.X    FP0,(A1)    ; put the element
  171.             ; back in RAM
  172.     MOVE.L    (A1),D2    ; now shift the high
  173.             ; Long word back (in
  174.             ; the output
  175.             ; matrix,and then put
  176.             ; it away in RAM
  177.     LSR.L    #8,D2
  178.     LSR.L    #8,D2
  179.     MOVE.L    D2,(A1)
  180.     MOVE.L    (A0),D2    ; now shift the high
  181.             ; Long word back (in
  182.             ; the input matrix)
  183.             ; and then put it
  184.             ; away
  185.     LSR.L    #8,D2
  186.     LSR.L    #8,D2
  187.     MOVE.L    D2,(A0)
  188.  
  189.     ADD    #12,A0    ; increment the
  190.             ; element's address
  191.             ; (to get the next
  192.             ; element)
  193.     ADD    #12,A1
  194.             
  195.     DBLT    D1,Loop    ; decrement the
  196.             ; number of columns
  197.             ; and test
  198. ; if we are here we have gone through one 
  199. ; column
  200.     MOVE.L    inMat(A6),A2
  201.     MOVE.W    2(A2),D1; restore the number
  202.             ; of columns
  203.     DBLT    D0,Loop    ; test if we have
  204.             ; completed through
  205.             ; all the rows
  206. ; if we are here, we have gone through all 
  207. ; the rows
  208.  
  209.  
  210. ; since we have done our work let’s put away 
  211. ; our toys (clear the stack of all the 
  212. ; garbage) and go home.
  213.  
  214.     MOVE.L    (A7)+,A3            ; pop off the old A3
  215.     MOVE.L    (A7)+,A2            ; pop off the old A2
  216.     UNLK A6            
  217.     MOVE.L    (A7)+,A0            ; save the return address
  218.     ADD.L    #12,A7    ; move the stack
  219.             ; pointer clear all
  220.             ; the data off the
  221.             ; stack
  222.             
  223.     CLR.W    (A7)    ; replace the return
  224.             ; value on the stack
  225.             ; with ours
  226.  
  227. ; ** NOTE: we are pushing a value of zero 
  228. ; onto the stack, meaning that nothing went 
  229. ; wrong.  This is bogus and in the real 
  230. ; version we need to fix this!
  231.  
  232.  
  233.     MOVE.L    A0,-(A7); push the return
  234.             ; address back onto
  235.             ; the stack
  236.             
  237.     RTS        ; return to reality
  238.             ; (our calling
  239.             ; program)
  240.     ENDF
  241.     END
  242.